package com.bjsxt.system.service.impl;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.bjsxt.common.util.PageInfoUtil;
import com.bjsxt.system.dao.ICateDao;
import com.bjsxt.system.dao.IReginDao;
import com.bjsxt.system.pojo.Cate;
import com.bjsxt.system.pojo.Regin;
import com.bjsxt.system.service.ISystemService;
@Service("systemService")
public class SystemService implements ISystemService
{
	@Resource
	private IReginDao reginDao;
	@Resource
	private ICateDao cateDao;
	
	//查询单个的区域
	@Override
	public Regin findOneRegin(Map<String, Object> condMap)
	{
		// TODO Auto-generated method stub
		return this.reginDao.findOne(condMap);
	}

	//添加单个区域
	@Override
	public JSONObject insertOneRegin(Regin regin)
	{
		JSONObject resultJson=new JSONObject();
		int res = this.reginDao.insertOne(regin);
		if(res>0)
		{
			resultJson.put("code", 1);
			resultJson.put("info", "添加区域成功");
			
			JSONObject dataJson=new JSONObject();
			dataJson.put("id", regin.getId());
			
			resultJson.put("date", dataJson);
		}else
		{
			resultJson.put("code", 0);
			resultJson.put("info", "添加区域失败");
		}
		return resultJson;
	}

	@Override
	public JSONObject updateOneRegin(Regin regin)
	{
		JSONObject resultJson=new JSONObject();
		int res = this.reginDao.updateOne(regin);
		if(res>0)
		{
			resultJson.put("code", 1);
			resultJson.put("info", "更新区域成功");
			
		}else
		{
			resultJson.put("code", 0);
			resultJson.put("info", "更新区域失败");
		}
		return resultJson;
	}

	@Override
	public List<Regin> findListRegin(PageInfoUtil pageInfo,Map<String, Object> condMap)
	{
		//先判断是否分页
		if(pageInfo!=null)
		{
			//先查询总记录数
			condMap.put("pageCount", true);
			if(condMap.get("keyword")!=null){
				condMap.put("keyword", "%"+condMap.get("keyword")+"%");
			}
			List<Regin> reginLi = this.reginDao.findCondList(condMap);
			int totalRe = reginLi.get(0).getId();
			
			//准备分页查询的数据
			condMap.put("pageCount", false);
			pageInfo.setTotalRecord(totalRe);
			int currentRecord = pageInfo.getCurrRecord();
			int pageSize = pageInfo.getPageSize();
			condMap.put("currentRecord", currentRecord);
			condMap.put("pageSize", pageSize);
			//查询分页数据
			List<Regin> reginList = this.reginDao.findCondList(condMap);
			return reginList;
		}else
		{
			//直接准备不分页查询数据
			condMap.put("keyword", "%"+condMap.get("keyword")+"%");
			List<Regin> reginList = this.reginDao.findCondList(condMap);
			return reginList;
		}
		
	}
	
	//查询单个的分类
	@Override
	public Cate findOneCate(Map<String, Object> condMap)
	{
		// TODO Auto-generated method stub
		return this.cateDao.findOne(condMap);
	}

	//添加单个分类
	@Override
	public JSONObject insertOneCate(Cate cate)
	{
		JSONObject resultJson=new JSONObject();
		int res = this.cateDao.insertOne(cate);
		if(res>0)
		{
			resultJson.put("code", 1);
			resultJson.put("info", "添加分类成功");
			
			JSONObject dataJson=new JSONObject();
			dataJson.put("id", cate.getId());
			
			resultJson.put("date", dataJson);
		}else
		{
			resultJson.put("code", 0);
			resultJson.put("info", "添加分类失败");
		}
		return resultJson;
	}

	@Override
	public JSONObject updateOneCate(Cate cate)
	{
		JSONObject resultJson=new JSONObject();
		int res = this.cateDao.updateOne(cate);
		if(res>0)
		{
			resultJson.put("code", 1);
			resultJson.put("info", "更新分类成功");
			
		}else
		{
			resultJson.put("code", 0);
			resultJson.put("info", "更新分类失败");
		}
		return resultJson;
	}

	@Override
	public List<Cate> findListCate(PageInfoUtil pageInfo,Map<String, Object> condMap)
	{
		//先判断是否分页
		if(pageInfo!=null)
		{
			//先查询总记录数
			condMap.put("pageCount", true);
			if(condMap.get("keyword")!=null){
				condMap.put("keyword", "%"+condMap.get("keyword")+"%");
			}
			List<Cate> cateLi = this.cateDao.findCondList(condMap);
			int totalRe = cateLi.get(0).getId();
			
			//准备分页查询的数据
			condMap.put("pageCount", false);
			pageInfo.setTotalRecord(totalRe);
			int currentRecord = pageInfo.getCurrRecord();
			int pageSize = pageInfo.getPageSize();
			condMap.put("currentRecord", currentRecord);
			condMap.put("pageSize", pageSize);
			//查询分页数据
			List<Cate> cateList = this.cateDao.findCondList(condMap);
			return cateList;
		}else
		{
			//直接准备不分页查询数据
			condMap.put("keyword", "%"+condMap.get("keyword")+"%");
			List<Cate> cateList = this.cateDao.findCondList(condMap);
			return cateList;
		}
		
	}
}
